﻿<%@ WebHandler Language="C#" Class="GetQRCode" %>
using Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WxPayAPI;
using System.Web.SessionState;
using ThoughtWorks.QRCode.Codec;
using System.Text;
//using ThoughtWorks.QRCode.Codec.Util;
using System.Drawing.Drawing2D;
using Newtonsoft.Json;



public class GetQRCode : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var data = "https://cli.im/tools";// context.Request["qcode"];

        string xxx = "";

        string path =context.Server.MapPath("/image/hb.png");

        if (!string.IsNullOrEmpty(data))
        {

            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//// 纠错级别，L (7%)、M (15%)、Q (25%)、H (30%)

            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 8;
            qrCodeEncoder.QRCodeBackgroundColor = Color.Green;//设置二维码背景颜色
            qrCodeEncoder.QRCodeForegroundColor = Color.White;//设置二维码前景色

            System.Drawing.Image image = qrCodeEncoder.Encode(data);
            //System.IO.MemoryStream MStream1 = new System.IO.MemoryStream();
            //CombinImage(image, context.Server.MapPath("../../Theme/img/logo.jpg")).Save(MStream1, System.Drawing.Imaging.ImageFormat.Png);

            //ThoughtWorks.QRCode 生成的四周没有留白的二维码图片，其右边和下边分别会多出 1 像素，使用以下方法来调整图片大小
            // 创建一个新的图片（宽度和高度各缩小 1 像素）
            Bitmap bitmap2 = new Bitmap(image.Size.Width - 1, image.Size.Height - 1);
            // 以新图片来绘图
            Graphics g2 = Graphics.FromImage(bitmap2);
            // 新旧图片绘制到新图片中（左上角对齐）
            g2.DrawImage(image, 0, 0);


            System.Drawing.Image image2;
            image2 = System.Drawing.Image.FromFile(path);
            Graphics graphics = Graphics.FromImage(image2);

            //　//对二维码进行重绘
            graphics.DrawImage(CombinImage(bitmap2, context.Server.MapPath("/image/hb.png")), new System.Drawing.Rectangle(545, 290, CombinImage(bitmap2, context.Server.MapPath("/image/hb.png")).Width, CombinImage(image,context.Server.MapPath("/image/hb.png")).Height));//第一个image(270, 520,)越小越往左、上,调图片位置，“-”的数字越小越往右、下，第二个image调整图大小，“+”调大


            System.IO.MemoryStream MStream = new System.IO.MemoryStream();
            image2.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);



            //释放画布
            graphics.Dispose();
            //设置输出的图片格式
           
            //image.Dispose();
            MStream.Dispose();
            //MStream1.Dispose();

            //            //裁切掉多余的 1 像素
            //ThoughtWorks.QRCode 生成的四周没有留白的二维码图片，其右边和下边分别会多出 1 像素，使用以下方法来调整图片大小


            //将 Bitmap 写入到流

            Stream stream = new MemoryStream();
            bitmap2.Save(stream, ImageFormat.Png);
            //若已裁切 1 像素，请修改为 bitmap2



            //将 Bitmap 保存到磁盘


           var  imgName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
            //获取相对于应用的基目录创建目录
            string imgPath = System.Web.Hosting.HostingEnvironment.MapPath("/image/");//System.AppDomain.CurrentDomain.baxxxxseDirectory + path;     //通过此对象获取文件名
            if (!Directory.Exists(imgPath))
            {
                Directory.CreateDirectory(imgPath);
            }
            bitmap2.Save(imgPath+imgName, ImageFormat.Jpeg);

             context.Response.Write(JsonConvert.SerializeObject(new { code=1,msg="ok",data="/image/"+imgName}));
            
            //            //若已裁切 1 像素，请修改为 bitmap2
        }
      




    }

    /// <summary>
    /// 调用此函数后使此两种图片合并，类似相册，有个
    /// 背景图，中间贴自己的目标图片
    /// </summary>
    /// <param name="imgBack">粘贴的源图片</param>
    /// <param name="destImg">粘贴的目标图片</param>
    public static System.Drawing.Image CombinImage(System.Drawing.Image imgBack, string destImg)
    {
        System.Drawing.Image img = System.Drawing.Image.FromFile(destImg);    //照片图片
        if (img.Height != 65 || img.Width != 65)
        {
            img = KiResizeImage(img, 65, 65, 0);
        }
        Graphics g = Graphics.FromImage(imgBack);
        g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);   //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
                                                                     //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框
                                                                     //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
        g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
        GC.Collect();
        return imgBack;
    }



    /// <summary>
    /// Resize图片
    /// </summary>
    /// <param name="bmp">原始Bitmap</param>
    /// <param name="newW">新的宽度</param>
    /// <param name="newH">新的高度</param>
    /// <param name="Mode">保留着，暂时未用</param>
    /// <returns>处理以后的图片</returns>
    public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)
    {
        try
        {
            System.Drawing.Image b = new Bitmap(newW, newH);
            Graphics g = Graphics.FromImage(b);
            // 插值算法的质量
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
            g.Dispose();
            return b;
        }
        catch
        {
            return null;
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

